﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace Estimate.Common.DataFileValidator
{
    public class SdxCityForeValidator:IDataFileValidator
    {

        public ValidateResult ValidateFile(string filepath)
        {

            ValidateResult result = new ValidateResult();

            string pattern = @"\ +";
            int[] floatIndexes = new int[] { 11, 12, 19, 20, 21 };

            StreamReader file = new StreamReader(filepath);

            string line = file.ReadLine();
            line = file.ReadLine();
            line = file.ReadLine();
            line = file.ReadLine();
            line = file.ReadLine();


            string stnum;
            string[] lineItems;

            int lineNumbers = 6;
            while (!file.EndOfStream)
            {
                line = file.ReadLine();
                lineItems = Regex.Split(line.TrimStart(), pattern, RegexOptions.IgnoreCase);

                if (lineItems.Length == 6)
                {
                    stnum = lineItems[0];

                    //确认当前预报站的记录行数
                    int lines = Convert.ToInt32(lineItems[4]);



                    for (int i = 0; i < lines; i++)
                    {
                        line = file.ReadLine();
                        lineItems = Regex.Split(line.TrimStart(), pattern, RegexOptions.IgnoreCase);

                        int curCol = 0;

                        try
                        {
                            for (int j = 0; j < floatIndexes.Length; j++)
                            {
                                curCol = floatIndexes[j];
                                float floatResult = Convert.ToSingle(lineItems[curCol].ToString());
                            }
                        }
                        catch (FormatException e)
                        {
                            string msg = string.Format("第{0}行，第{1}列数据不能转化为float数值类型。", lineNumbers, curCol);
                            ValidateResultRecord record = new ValidateResultRecord(lineNumbers, curCol, msg);
                            result.ErrorList.Add(record);
                        }
                        catch (Exception e)
                        {
                            string msg = e.Message;
                            ValidateResultRecord record = new ValidateResultRecord(lineNumbers, curCol, msg);
                            result.ErrorList.Add(record);
                        }
                        lineNumbers++;
                    }
                }

                lineNumbers++;
            }
            return result;
        }
    }
}
